﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AMS.DataAccess.Trademark.BO;
using AMS.DataAccess.BO;
using AMS.DataAccess.Interfaces;
using AMS.DataAccess.Trademark.Interfaces;

public partial class Cases_EditCaseApplicant : BaseWorkPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Utils.ParseIntQueryString(Request, "caseid", ref caseId))
        {
            GlobalTransfer.RedirectToErrorPage("参数不正确");
            return;
        }

        // TODO: Check for owner access here

        if (!IsPostBack)
        {
            DoDataBind();
            BindPageTitle();
        }
        else
        {
            LoadPostBackData();
        }
    }

    void LoadPostBackData()
    {
        hfAddedCustoemrID.Value = Request[hfAddedCustoemrID.UniqueID];
    }
    #region Properties
    int caseId;
    #endregion

    void DoDataBind()
    {
        ICaseApplicantManager caseAppMgr = ManagerFactory.CreateCaseApplicantManager();
        List<CaseApplicantInfo> appInfoList = caseAppMgr.GetCaseApplicants(CallContext, caseId);
        gvApp.DataSource = appInfoList;
        gvApp.DataKeyNames = new string[] { "ApplicantID" };
        gvApp.DataBind();

        lblNoRecordMsg.Visible = (appInfoList == null || appInfoList.Count == 0);
    }

    void BindPageTitle()
    {
        ICaseBasicInfoManager caseMgr = ManagerFactory.CreateCaseBasicInfoManager();
        CaseDisplayInfo caseInfo = caseMgr.GetCaseBasicInfo(CallContext, caseId);
        lblCaseName.Text = CaseUtils.FormatCaseTitle(caseInfo);
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        Server.Transfer("EditCaseTrademark.aspx" + Request.Url.Query);
    }

    protected void uc_OnApplicantAdded(object sender, EventArgs e)
    {
        DoDataBind();
        ucApp.Reset();
    }

    protected void lblEdit_Command(object sender, CommandEventArgs e)
    {
        int itemIndex = int.Parse(e.CommandArgument.ToString());
        int appId = Convert.ToInt32(gvApp.DataKeys[itemIndex].Value);
        ICaseApplicantManager appMgr = ManagerFactory.CreateCaseApplicantManager();
        CaseApplicantInfo appInfo = appMgr.GetCaseApplicant(CallContext, caseId, appId);
        ucApp.EditCaseApplicant(appInfo);
    }

    protected void lblDelete_Command(object sender, CommandEventArgs e)
    {
        int itemIndex = int.Parse(e.CommandArgument.ToString());
        int appId = Convert.ToInt32(gvApp.DataKeys[itemIndex].Value);
        ICaseApplicantManager appMgr = ManagerFactory.CreateCaseApplicantManager();
        if (appMgr.DeleteCaseApplicant(CallContext, caseId, appId))
        {
            MsgInfo("删除成功。");
        }

        DoDataBind();
    }

    protected void lblDeleteAppInfo_Command(object sender, CommandEventArgs e)
    {
        int itemIndex = int.Parse(e.CommandArgument.ToString());
        int appId = Convert.ToInt32(gvApp.DataKeys[itemIndex].Value);
        ICustomerManager customerMgr = ManagerFactory.CreateCustomerManager();
        ICaseApplicantManager appMgr = ManagerFactory.CreateCaseApplicantManager();
        try
        {
            appMgr.DeleteCaseApplicant(CallContext, caseId, appId);
            customerMgr.DeleteCustomer(CallContext, new int[] { appId });
            DoDataBind();
            MsgInfo("删除成功。");
        }
        catch (Exception ex)
        {
            MsgError("由于关联关系，申请信息将不允许删除。");
            // TODO: log to the system
        }
    }

    protected void btnGetApplicant_Click(object sender, EventArgs e)
    {
        int appId = int.Parse(hfAddedCustoemrID.Value);
        CaseApplicantInfo appInfo = new CaseApplicantInfo();
        appInfo.ApplicantID = appId;
        appInfo.CaseID = caseId;
        if (gvApp.Rows.Count == 0)
        {
            appInfo.IsFirst = true;
        }

        ICaseApplicantManager appMgr = ManagerFactory.CreateCaseApplicantManager();
        CaseApplicantInfo tmpInfo = appMgr.GetCaseApplicant(CallContext, caseId, appId);
        if (tmpInfo == null)
        {
            appMgr.AddCaseApplicant(CallContext, appInfo);
            DoDataBind();
            MsgInfo("添加成功。");
        }
        else
        {
            MsgWarning("申请人已经存在。");
        }
    }
}
